import java.util.ArrayList;

public class Location
{
	private final String NORTH = "NORTH";
	private final String EAST = "EAST";
	private final String SOUTH = "SOUTH";
	private final String WEST = "WEST";

	private int x; // +X is EAST
	private int y; // +Y is NORTH
	
	/*
	 * valid coordinates are either zero or positive
	 */
	public Location(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setXCoord(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setYCoord(int y) {
		this.y = y;
	}
	
	public boolean isValid() {
		if(x >= 0 && y >= 0) {
			return true;
		}
		return false;
	}
	
	/*
	 * @return the distance to otherLoc as a double
	 */
	public double distanceTo(Location otherLoc) {
		return Math.sqrt((x - otherLoc.getX())*(x - otherLoc.getX()) + (y - otherLoc.getY())*(y - otherLoc.getY()));
	}
		
	public String getDirectionTowards(Location otherLoc) {
		int xDist = x - otherLoc.getX();
		int yDist = y - otherLoc.getY();
		if(xDist > 0) {
			if(yDist > 0) {
				return SOUTH + WEST;
			} else if(yDist < 0) {
				return NORTH + WEST;
			} else {
				return WEST;
			}	
		} else if(xDist < 0) {
			if(yDist > 0) {
				return SOUTH + EAST;
			} else if(yDist < 0) {
				return NORTH + EAST;
			} else {
				return EAST;
			}	
		} else if(xDist == 0) {
			if(yDist > 0) {
				return SOUTH;
			} else if(yDist < 0) {
				return NORTH;
			}
		}
		return "ERROR: Same Location!";
	}
	
	public ArrayList<Location> getAdjacentLocations() {
		ArrayList<Location> locList = new ArrayList<Location>();
		for(int i = -1;i <= 1;i++) {
			for(int j = -1;j <= 1;j++) {
				Location newLoc = new Location(x+i,y+j);
				if(newLoc.isValid()) {
					locList.add(newLoc);
				}
			}
		}
		return locList;
	}
	
	/*
	 * @return an adjacent Location in the given direction
	 */
	public Location getLocationInDirection(String dir) {
		Location newLoc = null;
		if(dir.equals(NORTH)) {
			newLoc = new Location(x, y+1);
		} else if(dir.equals(SOUTH)) {
			newLoc = new Location(x, y-1);
		} else if(dir.equals(EAST)) {
			newLoc = new Location(x+1, y);
		} else if(dir.equals(WEST)) {
			newLoc = new Location(x-1, y);
		} else if(dir.equals(NORTH + EAST)) {
			newLoc = new Location(x+1, y+1);
		} else if(dir.equals(SOUTH + EAST)) {
			newLoc = new Location(x+1, y-1);
		} else if(dir.equals(SOUTH + WEST)) {
			newLoc = new Location(x-1, y-1);
		} else if(dir.equals(NORTH + WEST)) {
			newLoc = new Location(x-1, y+1);
		}
		return newLoc;
	}
	
	public boolean equals(Location otherLoc) {
		if(x == otherLoc.getX() && y == otherLoc.getY()) {
			return true;
		}
		return false;
	}

	public String toString() {
		return "(" + x + ", " + y + ")";
	}
}